Repository navigation
Use dynamic prime field with modulus sampled at runtime - #141
Merged
Merged
Conversation
frozenspider
force-pushed
the
fs/random-prime
branch
from
October 6, 2026 16:30
422fe57 to
b9767a0
Compare
# Conflicts: # crates/circuit/src/constraints.rs
frozenspider
force-pushed
the
fs/random-prime
branch
from
October 6, 2026 17:31
b9767a0 to
9796a7d
Compare
frozenspider
marked this pull request as ready for review
October 6, 2026 17:35
zkfriendly
requested changes
Oct 12, 2026
zkfriendly
left a comment
Collaborator
There was a problem hiding this comment.
The PR looks well structured overall. Cross-checked everything with the PoC repo and left some comments regarding correctness and soundness. Other stuff looks good (Prime candidate sampling, width selection, and verification look correct and compatible with bounds we have. Wide mul and Barret reduction look correct too.)
shreyas-londhe
removed their request for review
October 26, 2026 08:20
zkfriendly
approved these changes
Oct 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #136
Introduce$\le 126$ bit modulus that is squeezed from Fiat-Shamir transcript after the commitment and statement are bound. Number of bits to use in this prime depend on the claim shape.
DynField, a field withA lot of code is shared with
Fq, this shared code has been moved to a newhelpersmodule.This field relies on a globally shared modulus, and on an assumption that nobody would change that while field elements still exists. This convention is sadly not enforced by the type system.
[Remark: We could avoid this problem completely by passing around
FieldConfig, but that's a bit cumbersome]To sample a random prime, we use small prime numbers table sieve, followed by Miller-Rabin primality test, based on the PoC repo implementation.
E2E performance is quite close to static field, and can be optimized further. This is notable given that P/V can no longer do prime projection at the setup phase (setup got a bit faster).
Notable tricks used:
Vec<_>, withLayoutspecifying the sparse matrix layout.BigInttoFieldprojection has been optimized foru128-wide cases (in practice - pretty much all of them)Performance
To measure performance, I used a circuit
sha256-2kbRUSTFLAGS="-C target-cpu=native" cargo run --release -p bitz-cli -- circuit-e2e --circuit sha256-2kbsha256-2kbNotes:
mainbranch we normally useFq<Q100>, a 100-bit prime. To make it comparable, I usedFq<2230695117703373733444658220249201783>